package com.houshuai.emqtt;

import com.houshuai.emqtt.callback.PushCallBack;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.integration.annotation.IntegrationComponentScan;

/**
 * {@link IntegrationComponentScan} 进行扫描 integ生命的注解并实现
 *
 */
@SpringBootApplication
@IntegrationComponentScan
public class EmqttApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmqttApplication.class, args);
        try {
            // 注册到broker，以消费者模型注册   
            MqttClient mqttClient = new MqttClient("ws://localhost:8083/mqtt","idea-cli");
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            options.setUserName("idea-cli");
            options.setPassword("public".toCharArray());
            options.setConnectionTimeout(1000);
            options.setKeepAliveInterval(1000);
            mqttClient.connect(options);
            // 监听订阅的topic
            mqttClient.setCallback(new PushCallBack());
            // 订阅topic
            mqttClient.subscribe("hello",1);
        } catch (MqttException e) {
            e.printStackTrace();
        }
        System.out.println("(♥◠‿◠)ﾉﾞ  uap mqtt消息模块启动成功   ლ(´ڡ`ლ)ﾞ  \n");
    }
}
